package com.live.play

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import com.tencent.qgame.animplayer.AnimView
import com.tencent.qgame.animplayer.util.ScaleType

class MainActivity : AppCompatActivity() {

    var myAnimView:AnimView?=null

    lateinit var webView :WebView

    lateinit var button1: Button
    lateinit var button2: Button
    lateinit var button3: Button
    lateinit var button4: Button

    lateinit var button5: Button
    lateinit var button6: Button
    lateinit var button7: Button
    lateinit var button8: Button
    lateinit var button9: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        myAnimView = findViewById(R.id.anim_view)
        supportActionBar?.hide();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


        val display = windowManager.defaultDisplay
        val width = display.width
        val height = display.height
        val params = myAnimView?.layoutParams
        params?.width = dpToPx(width.toFloat())
//        params?.height = dpToPx(height.toFloat())
//        showMp4FileAnim("video.mp4")
        initWebView()

        button1 = findViewById(R.id.flower)
        button2 = findViewById(R.id.rain)
        button3 = findViewById(R.id.heart)
        button4 = findViewById(R.id.snow)

        button1.setOnClickListener{
            val gameUrl = "file:///android_asset/flower/index.html"
            webView.loadUrl(gameUrl)
        }
        button2.setOnClickListener{
            val gameUrl = "file:///android_asset/love/index.html"
            webView.loadUrl(gameUrl)
        }

        button3.setOnClickListener{
            val gameUrl = "file:///android_asset/heart/index.html"
            webView.loadUrl(gameUrl)
        }
        button3.setOnClickListener{
            val gameUrl = "file:///android_asset/heart/index.html"
            webView.loadUrl(gameUrl)
        }

        button4.setOnClickListener{
            val gameUrl = "file:///android_asset/snow/index.html"
            webView.loadUrl(gameUrl)
        }

        button5 = findViewById(R.id.yinhua_flower)
        button6 = findViewById(R.id.snowAll)

        button8 = findViewById(R.id.lightChange)

        button7 = findViewById(R.id.change1)
//        button9 = findViewById(R.id.change2)

        button5.setOnClickListener{
            val gameUrl = "file:///android_asset/cherryblossomrain/index.html"
            webView.loadUrl(gameUrl)
        }
        button6.setOnClickListener{
            val gameUrl = "file:///android_asset/fallingsnowfalling/index.html"
            webView.loadUrl(gameUrl)
        }
        button7.setOnClickListener{
//            val gameUrl = "file:///android_asset/gradientbackground/index.html"
//            webView.loadUrl(gameUrl)
            myAnimView?.removeAllViews()
            showMp4FileAnim("video.mp4")
        }
        button8.setOnClickListener{
            webView.loadUrl("about:blank")
            myAnimView?.removeAllViews()
        }

//        button9.setOnClickListener{
//            myAnimView?.removeAllViews()
//            showMp4FileAnim("video2.mp4")
//        }
    }

    private fun dpToPx(dpValue: Float): Int {
        val scale = resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    @SuppressLint("SetJavaScriptEnabled")
     fun initWebView() {
        webView = findViewById(R.id.webView)
        // WebView设置
        val webSettings: WebSettings = webView.settings
        // 可选，设置开启Chrome调试
        WebView.setWebContentsDebuggingEnabled(true)
        // 设置屏幕自适应
        val display = windowManager.defaultDisplay
        val width = display.width
        val height = display.height
//        webView.layoutParams.width = width
//        webView.layoutParams.height = height
        webSettings.useWideViewPort = true
        webSettings.loadWithOverviewMode = true
        // 建议禁止缓存加载，以确保在攻击发生时可快速获取最新的阿里云验证码2.0进行对抗
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        // 设置WebView组件支持加载JavaScript
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true

        // 设置背景色
        webView.setBackgroundColor(0)
        // 设置透明度 范围：0-255
        webView.background.alpha = 0

        webView.webViewClient = object : WebViewClient(){
            override fun shouldOverrideUrlLoading(view: WebView, url: kotlin.String): kotlin.Boolean{
                view.loadUrl(url)
                return true
            }
        }
//        val gameUrl = "file:///android_asset/flower/index.html"
        webView.loadUrl("about:blank")
//        webView.loadUrl(gameUrl)
    }

    private fun showMp4FileAnim(path: String) {
        val animView = AnimView(this)
        animView.setLoop(1)
        animView.setScaleType(ScaleType.CENTER_CROP)
        val params = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        animView.layoutParams = params
        myAnimView?.addView(animView)
        Mp4AnimUtils.playAssets(
            path,
            animView,
            object : Mp4AnimUtils.OnPlayCallback {
                override fun onStart() {}
                override fun onFailed() {
                    if (isDestroyed) return
                    myAnimView?.removeView(animView)
                }
                override fun onVideoComplete() {
                    if (isDestroyed) return
                    myAnimView?.removeView(animView)
                }
            },
            true
        )
    }
}